package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * @author : [wangtao]
 * @className : EmployeeController
 * @description : [employee的控制层类]
 * @createTime : [2022/10/6 17:38]
 * @updateTime : [2022/10/6 17:38]
 */

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
//登录
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String name = employee.getUsername();
        String password = employee.getPassword();
//    String pwd = MD5Config.md5Production(password);
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
//    System.out.println(pwd);

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername()).eq(Employee::getPassword, pwd);
        Employee list = employeeService.getOne(lqw);
        if (list == null) {
            return R.error("登录失败");
        }
        if (list.getStatus() == 0) {
            return R.error("用户被禁用");
        }
        request.getSession().setAttribute("employee", list.getId());
        return R.success(list);
    }
/*
 * 退出登录 ,要将session 移除
 */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.removeAttribute("employee");
        return R.success("退出成功");
    }
    /*
     * 添加用户
     */
    @PostMapping
    public R<String> add(@RequestBody Employee employee, HttpServletRequest request) {
    log.info(employee.toString());

        boolean flag = false;
//            Long emp = (long) request.getSession().getAttribute("employee");
//            employee.setCreateTime(LocalDateTime.now());
//            employee.setUpdateTime(LocalDateTime.now());
            employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//            employee.setCreateUser(emp);
//            employee.setUpdateUser(emp);
            flag = employeeService.save(employee);
        if (flag) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }
    /*
     * 分页方法
     */
    @GetMapping("/page")
    public R<Page> pageControl(int page,int pageSize,String name){
        log.info("page= {},pageSize= {},name= {}",page,pageSize,name);
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        lqw.orderByDesc(Employee::getUpdateTime);
        Page pageIn = employeeService.page(pageInfo, lqw);
        return R.success(pageIn);
    }


    /*
     * 员工状态改变
     */
    @PutMapping
    public R<String> putStatus(@RequestBody Employee employee,HttpServletRequest request){
        employee.setUpdateTime(LocalDateTime.now());
        long index = (long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(index);
        boolean b = employeeService.updateById(employee);
        if(b){
            return R.success("更新成功");
        }
        return R.error("更新失败");
    }
    /*
     * @author wangtao
     * @description 数据回显函数
     * Function: edit
     * @param [id]
     * @return com.itheima.reggie.common.R<com.itheima.reggie.entity.Employee>
     * @createTime  2022/10/8 19:20
     */
    @GetMapping("/{id}")
    public R<Employee> edit(@PathVariable Long id) {
        log.info("id = {}",id);
        Employee emp = employeeService.getById(id);
        if (emp != null) {
            return R.success(emp);
        }
        return R.error("没有该用户");
    }

}
